<!DOCTYPE html>
<html>
<head>
    {{template "header"}}
</head>
<body>
<script>
    let tableid = webix.uid()
    let getColumns = function () {
        return [
            {view: "text", name: "name", label: gtr("Name"), css: "nborder-input",},
            {view: "text", name: "oid", label: tr("cwmp","OID"), css: "nborder-input",},
            {view: "text", name: "software_version", label: tr("cwmp","Software version"), css: "nborder-input",},
            {view: "text", name: "oui", label: tr("cwmp","OUI"), css: "nborder-input",},
            {view: "text", name: "product_class", label: tr("cwmp","Product class"), css: "nborder-input",},
            {view: "label", label: tr("cwmp","Config content")},
            {name: "content", view: "codemirror-editor", mode: "shell"},
        ]
    }

    let deleteItem = function (ids, callback) {
        webix.confirm({
            title: gtr("Operation confirmation"),
            ok: gtr("Yes"), cancel: gtr("No"),
            text: gtr("Confirm to delete? This operation is irreversible."),
            callback: function (ev) {
                if (ev) {
                    webix.ajax().get('/admin/cwmp/factoryreset/delete', {ids: ids}).then(function (result) {
                        let resp = result.json();
                        webix.message({type: resp.msgtype, text: resp.msg, expire: 2000});
                        if (callback)
                            callback()
                    }).fail(function (xhr) {
                        webix.message({type: 'error', text: gtr("Delete Failure:") + xhr.statusText, expire: 2000});
                    });
                }
            }
        });
    }

    
    webix.ready(function () {
        let queryid = webix.uid();
        let tableid = webix.uid();
        let uploadid = webix.uid();
        let reloadData = wxui.reloadDataFunc(tableid, "/admin/cwmp/factoryreset/query", queryid)
        webix.ui({
            css: "main-panel",
            padding: 7,
            rows: [
                wxui.getPageToolbar({
                    title: tr("cwmp","Factoryreset config management"),
                    icon: "mdi mdi-file-cog",
                    elements: [
                        wxui.getPrimaryButton(gtr("Edit"), 90, false, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                vitem.api_pwd = ""
                                wxui.openFormWindow({
                                    fullscreen: true,
                                    width: 640,
                                    height: 720,
                                    title: tr("Edit Factoryreset config"),
                                    data: vitem,
                                    post: "/admin/cwmp/factoryreset/update",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Clone"), 90, false, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                vitem._id = ""
                                vitem.id = ""
                                wxui.openFormWindow({
                                    fullscreen: true,
                                    width: 640,
                                    height: 720,
                                    title: tr("cwmp","Clone Factoryreset config"),
                                    data: vitem,
                                    post: "/admin/cwmp/factoryreset/add",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Create"), 90, false, function () {
                            wxui.openFormWindow({
                                fullscreen: true,
                                width: 640,
                                height: 720,
                                title: tr("cwmp","Create Factoryreset config"),
                                post: "/admin/cwmp/factoryreset/add",
                                callback: reloadData,
                                elements: getColumns()
                            }).show();
                        }),
                        wxui.getDangerButton(gtr("Remove"), 90, false, function () {
                            let rows = wxui.getTableCheckedIds(tableid);
                            if (rows.length === 0) {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            } else {
                                deleteItem(rows.join(","), reloadData);
                            }
                        }),
                    ],
                }),
                wxui.getTableQueryCustomForm(queryid, [
                    {
                        cols: [
                            {view: "search", id: "keyword", name: "keyword", placeholder: "keywords", width: 320},
                            {
                                view: "button",
                                label: gtr("Query"),
                                css: "webix_transparent",
                                type: "icon",
                                icon: "mdi mdi-search-web",
                                borderless: true,
                                width: 100,
                                click: function () {
                                    reloadData()
                                }
                            }, {}
                        ]
                    }
                ]),
                wxui.getDatatable({
                    tableid: tableid,
                    url: '/admin/cwmp/factoryreset/query',
                    columns: [
                        {
                            id: "state",
                            header: {content: "masterCheckbox", css: "center"},
                            headermenu: false,
                            width: 45,
                            css: "center",
                            template: "{common.checkbox()}"
                        },
                        {
                            id: "name",
                            header: [tr("cwmp","Name")],
                            fillspace: true,
                            sort: "server",
                        },
                        {
                            id: "oid",
                            header: [tr("cwmp","OID")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "software_version",
                            header: [tr("cwmp","Software version")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "oui",
                            header: [tr("cwmp","OUI")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "product_class",
                            header: [tr("cwmp","Product class")],
                            adjust: true,
                            sort: "server",
                        },
                        {id: "none", header: [""], fallspace: true},
                        // {header: {content: "headerMenu"}, headermenu: false, width: 35}
                    ],
                    leftSplit: 1,
                    rightSplit: 0,
                    pager: true,
                }),
                wxui.getTableFooterBar({
                    tableid: tableid,
                    callback: reloadData,
                    actions: [],
                }),
            ]
        })
    })
</script>
</body>
</html>